package org.linlinjava.litemall.admin.service.order_manage;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tencentcloudapi.bda.v20200324.models.Age;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.linlinjava.litemall.admin.enums.AdminErrorCode;
import org.linlinjava.litemall.admin.vo.locks.*;
import org.linlinjava.litemall.core.util.InventoryUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.config.CenterServerAddress;
import org.linlinjava.litemall.db.config.ServerAddress;
import org.linlinjava.litemall.db.dao.*;
import org.linlinjava.litemall.db.dao.order_manage.AgedmallLocksMapper;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.jsonfield.LitemallGoodsProductSpecification;
import org.linlinjava.litemall.db.service.rfid.AgedmallLgRfidStateService;
import org.linlinjava.litemall.db.vo.locks.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.xml.ws.ResponseWrapper;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @author wj
 * @date 2021/8/11 10:45
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AgedmallLocksService {


    @Resource
    private AgedmallStoreHospitalMapper agedmallStoreHospitalMapper;

    @Resource
    private AgedmallLocksMapper agedmallLocksMapper;

    @Resource
    private LitemallGoodsMapper litemallGoodsMapper;

    @Resource
    private AgedmallChargeTemplateMapper agedmallChargeTemplateMapper;

    @Resource
    private AgedmallRfidGoodsProductMapper agedmallRfidGoodsProductMapper;

    @Resource
    private AgedmallCabinetMapper agedmallCabinetMapper;

    @Resource
    private AgedmallScanGunsMapper agedmallScanGunsMapper;

    @Resource
    private AgedmallStoreMapper agedmallStoreMapper;

    @Resource
    private LitemallGoodsProductMapper litemallGoodsProductMapper;

    @Resource
    private AgedmallStorePositionMapper agedmallStorePositionMapper;

    @Resource
    private AgedmallVersionMapper agedmallVersionMapper;

    @Resource
    private AgedmallGoodsEquMapper agedmallGoodsEquMapper;

    @Resource
    private InventoryUtil inventoryUtil;

    @Resource
    private AgedmallCabinetGoodsMapper agedmallCabinetGoodsMapper;


    @Resource
    private AgedmallLgRfidStateService agedmallLgRfidStateService;


    @Resource
    private ServerAddress  serverAddress;




    public Object getResultMessage(String result){
        JSONObject jsonObject = JSON.parseObject(result);
        Object data =jsonObject.get("data") ;
        String msg =(String)jsonObject.get("msg") ;
        int code =(int)jsonObject.get("code") ;
        if(code!=0){
            return ResponseUtil.fail(code,msg);
        }
        else if(ObjectUtil.isNotNull(data)){
            return ResponseUtil.ok(data);
        }
        else {
            return ResponseUtil.ok();
        }
    }


    public int getResultCode(String result){
        JSONObject jsonObject = JSON.parseObject(result);
        return (int)jsonObject.get("code") ;

    }

    public Object addControl(AddControlVo addControlVo){
        AgedmallStoreHospitalExample hospitalExample=new AgedmallStoreHospitalExample();
        hospitalExample.or().andStoreIdEqualTo(addControlVo.getStoreId().longValue());
        List<AgedmallStoreHospital> agedmallStoreHospitals = agedmallStoreHospitalMapper.selectByExample(hospitalExample);
        if(agedmallStoreHospitals.isEmpty()){
            return ResponseUtil.fail(AdminErrorCode.STORE_HOSPITAL_NOT_EXIST.getCode(),
                    AdminErrorCode.STORE_HOSPITAL_NOT_EXIST.getMessage() );
        }
        AddPileVo addPileVo=new AddPileVo();
        addPileVo.setPileCode(addControlVo.getControlCode());
        addPileVo.setHospitalId(agedmallStoreHospitals.get(0).getHospitalId().intValue());
        AgedmallStorePositionExample storePositionExample=new AgedmallStorePositionExample();
        storePositionExample.or().andPositionEqualTo(addControlVo.getPosition()).andStoreIdEqualTo(addControlVo.getStoreId());
        AgedmallStorePosition storePosition=agedmallStorePositionMapper.selectOneByExample(storePositionExample);
        if(storePosition==null){
            AgedmallStorePosition agedmallStorePosition=new AgedmallStorePosition();
            agedmallStorePosition.setStoreId(addControlVo.getStoreId());
            agedmallStorePosition.setPosition(addControlVo.getPosition());
            agedmallStorePosition.setAddTime(LocalDateTime.now());
            agedmallStorePositionMapper.insertSelective(agedmallStorePosition);
        }

        String[] pildIds=addControlVo.getControlCode().split(",");
        List<Long> longHomepileId=new ArrayList<>();
        for (int i = 0; i <pildIds.length; i++) {
            long hid=Long.parseLong(pildIds[i]);
            if(Integer.MAX_VALUE<=hid){
                return ResponseUtil.fail(AdminErrorCode.NUMBER_CODE_OUT_RANGE.getCode(),AdminErrorCode.NUMBER_CODE_OUT_RANGE.getMessage());
            }
            longHomepileId.add(hid);
        }
        List<AgedmallGoodsEqu> equs=new ArrayList<>();
        AgedmallGoodsEquExample goodsEquExample=new AgedmallGoodsEquExample();
        goodsEquExample.or().andEquIdIn(longHomepileId).andTypeEqualTo(1).andDeletedEqualTo((byte)0);
        List<AgedmallGoodsEqu> isExistEquId = agedmallGoodsEquMapper.selectByExample(goodsEquExample);
        if(!isExistEquId.isEmpty()){
            return ResponseUtil.fail(AdminErrorCode.CONTROL_IS_EXIST.getCode(),AdminErrorCode.CONTROL_IS_EXIST.getMessage());
        }
        for (int i = 0; i <longHomepileId.size() ; i++) {
            AgedmallGoodsEqu goodsEqu=new AgedmallGoodsEqu();
            goodsEqu.setEquId(longHomepileId.get(i));
            goodsEqu.setStoreId(addControlVo.getStoreId().longValue());
            goodsEqu.setType(1);
            goodsEqu.setState(0);
            goodsEqu.setAddTime(LocalDateTime.now());
            equs.add(goodsEqu);
        }
        addPileVo.setPileLocation(addControlVo.getPosition());
        String result=HttpRequest.post(serverAddress.getCenter()+"admin_api/addPileBatch")
                //头信息，多个头信息多次调用此方法即可
                .header(CenterServerAddress.KEY_HEADER_STRING,CenterServerAddress.KEY_SECUREUTIL_MD5)
                .body(JSON.toJSONString(addPileVo))
                .timeout(50000)//超时，毫秒
                .execute().body();
        if(getResultCode(result)==0){
            agedmallLocksMapper.insertGoodsEquList(equs);
        }
        return getResultMessage(result);
    }


    public Object getAllDsc(){
        String result = HttpRequest.post(serverAddress.getCenter()+"admin_api/getDscTypes")
                //头信息，多个头信息多次调用此方法即可
                .header(CenterServerAddress.KEY_HEADER_STRING,CenterServerAddress.KEY_SECUREUTIL_MD5)
                .timeout(50000)//超时，毫秒
                .execute().body();
        return getResultMessage(result);
    }

    public Object controlList(ControlList controlList){
        if(ObjectUtil.isNull(controlList.getAddress())){
            controlList.setAddress(new ChainAddress());
        }
        List<Integer> hospitalIds=new ArrayList<>();
        if(controlList.getStoreId()!=null){
            AgedmallStoreHospitalExample hospitalExample=new AgedmallStoreHospitalExample();
            hospitalExample.or().andStoreIdEqualTo(controlList.getStoreId().longValue());
            List<AgedmallStoreHospital> agedmallStoreHospitals = agedmallStoreHospitalMapper.selectByExample(hospitalExample);
            if(agedmallStoreHospitals.isEmpty()){
                return ResponseUtil.fail(AdminErrorCode.STORE_HOSPITAL_NOT_EXIST.getCode()
                        ,AdminErrorCode.STORE_HOSPITAL_NOT_EXIST.getMessage());
            }
            int hospitalId=agedmallStoreHospitals.get(0).getHospitalId().intValue();
            hospitalIds.add(hospitalId);
        }
        else{
            AgedmallStoreExample storeExample=new AgedmallStoreExample();
            storeExample.or().andBusiIdEqualTo(controlList.getShopId()).andDeletedEqualTo((byte)0);
            List<AgedmallStore> agedmallStores = agedmallStoreMapper.selectByExample(storeExample);
            List<Long> storeIds=new ArrayList<>();
            for (int i = 0; i < agedmallStores.size(); i++) {
                storeIds.add(agedmallStores.get(i).getId());
            }
            if(storeIds.isEmpty()){
                Map<String,Object> map=new HashMap<>();
                map.put("total",0);
                return ResponseUtil.ok(map);
            }
            AgedmallStoreHospitalExample hospitalExample=new AgedmallStoreHospitalExample();
            hospitalExample.or().andStoreIdIn(storeIds);
            List<AgedmallStoreHospital> agedmallStoreHospitals = agedmallStoreHospitalMapper.selectByExample(hospitalExample);
            for (int i = 0; i < agedmallStoreHospitals.size(); i++) {
                hospitalIds.add(agedmallStoreHospitals.get(i).getHospitalId().intValue());
            }
        }

        PileListVo pileListVo=new PileListVo();
        pileListVo.setId(hospitalIds);
        pileListVo.setHpId(controlList.getEquipmentCode());
        pileListVo.setStat(controlList.getState());
        pileListVo.setGoodsId(controlList.getCommdityId());
        pileListVo.setPage(controlList.getPage());
        pileListVo.setLimit(controlList.getLimit());
        pileListVo.setProvinceId(controlList.getAddress().getProvince());
        pileListVo.setCityId(controlList.getAddress().getCity());
        pileListVo.setAreaId(controlList.getAddress().getArea());
        String result = HttpRequest.post(serverAddress.getCenter()+"admin_api/getPileList")
                //头信息，多个头信息多次调用此方法即可
                .header(CenterServerAddress.KEY_HEADER_STRING,CenterServerAddress.KEY_SECUREUTIL_MD5)
                .body(JSON.toJSONString(pileListVo))
                .timeout(50000)//超时，毫秒
                .execute().body();
        return getResultMessage(result);
    }



    public Object addRFID(AddRFIDVo addRFIDVo){
        String[] equIds=addRFIDVo.getEquipmentCode().split(",");
        List<AgedmallGoodsEqu> equs=new ArrayList<>();
        List<LitemallGoodsProductSpecification> skuFormatDtos=new ArrayList<>();
        for (int i = 0; i < addRFIDVo.getSkuInfo().size(); i++) {
            SkuInfoVo skuInfoVo = addRFIDVo.getSkuInfo().get(i);
            LitemallGoodsProductSpecification formatDto=new LitemallGoodsProductSpecification();
            formatDto.setId(skuInfoVo.getSpecId());
            formatDto.setName(skuInfoVo.getSpecName());
            formatDto.setInfoId(skuInfoVo.getSkuId());
            formatDto.setInfoName(skuInfoVo.getSkuName());
            skuFormatDtos.add(formatDto);
        }
        LitemallGoodsProduct  GoodsProduct=new LitemallGoodsProduct();
        if(addRFIDVo.getSkuInfo().size()>0){
            //判断是否存在
            LitemallGoodsProductExample goodsProductExample=new LitemallGoodsProductExample();
            goodsProductExample.or().andGoodsIdEqualTo(addRFIDVo.getCommodityId());
            List<LitemallGoodsProduct> litemallGoodsProduct = litemallGoodsProductMapper.selectByExample(goodsProductExample);
            for (int i = 0; i < litemallGoodsProduct.size(); i++) {
                boolean isTrue=true;
                List<LitemallGoodsProductSpecification> productSpecifications=
                        JSONArray.parseArray(litemallGoodsProduct.get(i).getSpecifications(),LitemallGoodsProductSpecification.class);
                for (int k = 0; k < skuFormatDtos.size(); k++) {
                    if(!productSpecifications.contains(skuFormatDtos.get(k))){
                        isTrue=false;
                        break;
                    }
                }
                if(isTrue){
                    GoodsProduct=litemallGoodsProduct.get(i);
                }
            }
            if(ObjectUtil.isNull(GoodsProduct)||GoodsProduct.getId()==null){
                return ResponseUtil.fail(AdminErrorCode.GOODS_PRODUCT_NOT_EXIST.getCode()
                        ,AdminErrorCode.GOODS_PRODUCT_NOT_EXIST.getMessage());
            }
        }
        List<Long> longEquId=new ArrayList<>();
        for (int i = 0; i < equIds.length; i++) {
            long equId=Long.parseLong(equIds[i]);
            if(Integer.MAX_VALUE<=equId){
                return ResponseUtil.fail(AdminErrorCode.RFID_IS_EXIST.getCode(),AdminErrorCode.RFID_IS_EXIST.getMessage());
            }
            longEquId.add(equId);
        }
        AgedmallGoodsEquExample goodsEquExample=new AgedmallGoodsEquExample();
        goodsEquExample.or().andEquIdIn(longEquId).andTypeEqualTo(0).andDeletedEqualTo((byte)0);
        List<AgedmallGoodsEqu> isExistEquId = agedmallGoodsEquMapper.selectByExample(goodsEquExample);
        if(!isExistEquId.isEmpty()){
            return ResponseUtil.fail(AdminErrorCode.RFID_IS_EXIST.getCode(),AdminErrorCode.RFID_IS_EXIST.getMessage());
        }
        for (int i = 0; i <longEquId.size() ; i++) {
            AgedmallGoodsEqu goodsEqu=new AgedmallGoodsEqu();
            goodsEqu.setGoodsId(addRFIDVo.getCommodityId().longValue());
            goodsEqu.setEquId(longEquId.get(i));
            goodsEqu.setStoreId(addRFIDVo.getStoreId().longValue());
            goodsEqu.setCommditySkuName(addRFIDVo.getSkuName());
            goodsEqu.setType(0);
            goodsEqu.setState(0);
            goodsEqu.setAddTime(LocalDateTime.now());
            goodsEqu.setOnlineTime(LocalDateTime.now());
            goodsEqu.setGoodsProductId(ObjectUtil.isNull(GoodsProduct)||GoodsProduct.getId()==null?null:GoodsProduct.getId().longValue());
            equs.add(goodsEqu);
            //新增agedmall_lg_rfid_goods_cur_stat
            agedmallLgRfidStateService.addLgRfidGoods(addRFIDVo.getStoreId().longValue(),longEquId.get(i).toString(),1);
        }



        //绑定到医院
        AgedmallStoreHospitalExample hospitalExample=new AgedmallStoreHospitalExample();
        hospitalExample.or().andStoreIdEqualTo(addRFIDVo.getStoreId().longValue());
        List<AgedmallStoreHospital> agedmallStoreHospitals = agedmallStoreHospitalMapper.selectByExample(hospitalExample);
        Integer equType = agedmallLocksMapper.getGoodsCategoryId(addRFIDVo.getCommodityId());
        if(equType==null){
            return ResponseUtil.fail(AdminErrorCode.EQU_TYPE_NOT_EXIST.getCode(),AdminErrorCode.EQU_TYPE_NOT_EXIST.getMessage());
        }
        AddEquVo addEquVo=new AddEquVo();
        addEquVo.setHospitalId(agedmallStoreHospitals.get(0).getHospitalId().intValue());
        addEquVo.setEquipType(equType);
        addEquVo.setEquipCode(addRFIDVo.getEquipmentCode());
        String result=HttpRequest.post(serverAddress.getCenter()+"admin_api/addEquBatch")
                //头信息，多个头信息多次调用此方法即可
                .header(CenterServerAddress.KEY_HEADER_STRING,CenterServerAddress.KEY_SECUREUTIL_MD5)
                .body(JSON.toJSONString(addEquVo))
                .timeout(50000)//超时，毫秒
                .execute().body();
        if(getResultCode(result)==0){
            agedmallLocksMapper.insertGoodsEquList(equs);
        }
        inventoryUtil.add(addRFIDVo.getStoreId(),0,1,addRFIDVo.getCommodityId(),
                ObjectUtil.isNull(GoodsProduct)||GoodsProduct.getId()==null?null:GoodsProduct.getId());
        return getResultMessage(result);
    }


    public Object chainList(ChainListVo chainListVo){
        if(ObjectUtil.isNull(chainListVo.getAddress())){
            chainListVo.setAddress(new ChainAddress());
        }
        List<Integer> hospitalIds=new ArrayList<>();
        if(chainListVo.getStoreId()!=null){
            AgedmallStoreHospitalExample hospitalExample=new AgedmallStoreHospitalExample();
            hospitalExample.or().andStoreIdEqualTo(chainListVo.getStoreId().longValue());
            List<AgedmallStoreHospital> agedmallStoreHospitals = agedmallStoreHospitalMapper.selectByExample(hospitalExample);
            if(agedmallStoreHospitals.isEmpty()){
                return ResponseUtil.fail(AdminErrorCode.STORE_HOSPITAL_NOT_EXIST.getCode()
                        ,AdminErrorCode.STORE_HOSPITAL_NOT_EXIST.getMessage());
            }
            int hospitalId=agedmallStoreHospitals.get(0).getHospitalId().intValue();
            hospitalIds.add(hospitalId);
        }
        else{
            AgedmallStoreExample storeExample=new AgedmallStoreExample();
            storeExample.or().andBusiIdEqualTo(chainListVo.getShopId()).andDeletedEqualTo((byte)0);
            List<AgedmallStore> agedmallStores = agedmallStoreMapper.selectByExample(storeExample);
            List<Long> storeIds=new ArrayList<>();
            for (int i = 0; i < agedmallStores.size(); i++) {
                storeIds.add(agedmallStores.get(i).getId());
            }
            if(storeIds.isEmpty()){
                Map<String,Object> map=new HashMap<>();
                map.put("total",0);
                return ResponseUtil.ok(map);
            }
            AgedmallStoreHospitalExample hospitalExample=new AgedmallStoreHospitalExample();
            hospitalExample.or().andStoreIdIn(storeIds);
            List<AgedmallStoreHospital> agedmallStoreHospitals = agedmallStoreHospitalMapper.selectByExample(hospitalExample);
            for (int i = 0; i < agedmallStoreHospitals.size(); i++) {
                hospitalIds.add(agedmallStoreHospitals.get(i).getHospitalId().intValue());
            }
        }
        EquListVo equListVo=new EquListVo();
        equListVo.setId(hospitalIds);
        equListVo.setEquId(chainListVo.getEquipmentCode());
        equListVo.setStat(chainListVo.getState());
        equListVo.setGoodsId(chainListVo.getCommdityId());
        equListVo.setPage(chainListVo.getPage());
        equListVo.setLimit(chainListVo.getLimit());
        equListVo.setProvinceId(chainListVo.getAddress().getProvince());
        equListVo.setCityId(chainListVo.getAddress().getCity());
        equListVo.setAreaId(chainListVo.getAddress().getArea());
        String result=HttpRequest.post(serverAddress.getCenter()+"admin_api/chainList")
                //头信息，多个头信息多次调用此方法即可
                .header(CenterServerAddress.KEY_HEADER_STRING,CenterServerAddress.KEY_SECUREUTIL_MD5)
                .body(JSON.toJSONString(equListVo))
                .timeout(50000)//超时，毫秒
                .execute().body();
        return getResultMessage(result);
    }

    public Object changeControlState(ChangeControlStateVo changeControlStateVo){
        AgedmallGoodsEquExample goodsEquExample=new AgedmallGoodsEquExample();
        List<Long> equIdList=new ArrayList<>();
        for (int i = 0; i <changeControlStateVo.getIdList().size() ; i++) {
            equIdList.add(changeControlStateVo.getIdList().get(i).longValue());
        }
        AgedmallGoodsEqu goodsEqu=new AgedmallGoodsEqu();
        goodsEqu.setState(changeControlStateVo.getType());
        goodsEquExample.or().andTypeEqualTo(1).andEquIdIn(equIdList);
        agedmallGoodsEquMapper.updateByExampleSelective(goodsEqu,goodsEquExample);


        UpdatePileStatVo updatePileStatVo=new UpdatePileStatVo();
        updatePileStatVo.setIds(changeControlStateVo.getIdList());
        updatePileStatVo.setReason(changeControlStateVo.getReason());
        updatePileStatVo.setType(rfidStateTransform(changeControlStateVo.getType()));
        String result=HttpRequest.post(serverAddress.getCenter()+"admin_api/updatePileStat")
                //头信息，多个头信息多次调用此方法即可
                .header(CenterServerAddress.KEY_HEADER_STRING,CenterServerAddress.KEY_SECUREUTIL_MD5)
                .body(JSON.toJSONString(updatePileStatVo))
                .timeout(50000)//超时，毫秒
                .execute().body();
        return getResultMessage(result);
    }

    public Integer rfidStateTransform(Integer state){
        switch (state){
            case 1:
                return 2;
            case 2:
                return 3;
                //设备使用中
            case 3:
                return 5;
            case 0:
            default:
                return 1;
        }
    }

    public Integer exChangeRfidToLgRfidState(Integer state){
        switch (state){
            case 1:
                return 10;
            case 2:
                return 11;
            case 0:
            default:
                return 0;
        }
    }

    public Object changeRFIDState(ChangeRFIDStateVo changeRFIDStateVo){
        AgedmallGoodsEquExample goodsEquExample=new AgedmallGoodsEquExample();
        List<Long> equIdList=new ArrayList<>();
        for (int i = 0; i <changeRFIDStateVo.getIdList().size() ; i++) {
            equIdList.add(changeRFIDStateVo.getIdList().get(i).longValue());
            //修改agedmall_lg_rfid_goods_cur_stat状态
            agedmallLgRfidStateService.changeLgRfidState(changeRFIDStateVo.getIdList().get(i).toString(),exChangeRfidToLgRfidState(changeRFIDStateVo.getType()),1);
        }
        AgedmallGoodsEqu goodsEqu=new AgedmallGoodsEqu();
        goodsEqu.setState(changeRFIDStateVo.getType());
        if(changeRFIDStateVo.getType()==0){
            goodsEqu.setOnlineTime(LocalDateTime.now());
        }
        goodsEquExample.or().andTypeEqualTo(0).andEquIdIn(equIdList);
        List<AgedmallGoodsEqu> goodsEqus = agedmallGoodsEquMapper.selectByExample(goodsEquExample);
        agedmallGoodsEquMapper.updateByExampleSelective(goodsEqu,goodsEquExample);

        UpdateEquStatVo updateEquStatVo=new UpdateEquStatVo();
        updateEquStatVo.setIds(changeRFIDStateVo.getIdList());
        updateEquStatVo.setReason(changeRFIDStateVo.getReason());
        updateEquStatVo.setType(rfidStateTransform(changeRFIDStateVo.getType()));
        String result=HttpRequest.post(serverAddress.getCenter()+"admin_api/updateEquStat")
                //头信息，多个头信息多次调用此方法即可
                .header(CenterServerAddress.KEY_HEADER_STRING,CenterServerAddress.KEY_SECUREUTIL_MD5)
                .body(JSON.toJSONString(updateEquStatVo))
                .timeout(50000)//超时，毫秒
                .execute().body();
        for (int i = 0; i < goodsEqus.size(); i++) {
            AgedmallGoodsEqu inventoryGoods=goodsEqus.get(i);
            if(changeRFIDStateVo.getType()==0){
                inventoryUtil.add(inventoryGoods.getStoreId().intValue(),0,1,inventoryGoods.getGoodsId().intValue()
                        ,inventoryGoods.getGoodsProductId().intValue());
            }
            else{
                inventoryUtil.remove(inventoryGoods.getStoreId().intValue(),0,1,inventoryGoods.getGoodsId().intValue()
                        ,inventoryGoods.getGoodsProductId().intValue());
            }
        }
        return getResultMessage(result);
    }


    public Object replaceRFID(ReplaceRfidVo replaceRfidVo){
        AgedmallGoodsEquExample goodsEquExample=new AgedmallGoodsEquExample();
        goodsEquExample.or().andEquIdEqualTo(replaceRfidVo.getNewRFID()).andTypeEqualTo(0).andDeletedEqualTo((byte)0);
        List<AgedmallGoodsEqu> isExistEquId = agedmallGoodsEquMapper.selectByExample(goodsEquExample);
        if(!isExistEquId.isEmpty()){
            return ResponseUtil.fail(AdminErrorCode.RFID_IS_EXIST.getCode(), AdminErrorCode.RFID_IS_EXIST.getMessage());
        }
        AgedmallGoodsEquExample changeRfidEqu=new AgedmallGoodsEquExample();
        changeRfidEqu.or().andEquIdEqualTo(replaceRfidVo.getOldRFID()).andTypeEqualTo(0).andDeletedEqualTo((byte)0);
        AgedmallGoodsEqu changeGoodsEqu = agedmallGoodsEquMapper.selectOneByExample(changeRfidEqu);
        if(Integer.MAX_VALUE<=replaceRfidVo.getNewRFID()){
            return ResponseUtil.fail(AdminErrorCode.RFID_IS_EXIST.getCode(),AdminErrorCode.RFID_IS_EXIST.getMessage());
        }
        changeGoodsEqu.setEquId(replaceRfidVo.getNewRFID());
        agedmallGoodsEquMapper.updateByPrimaryKeySelective(changeGoodsEqu);
        String result=HttpRequest.post(serverAddress.getCenter()+"admin_api/replaceRFID")
                //头信息，多个头信息多次调用此方法即可
                .header(CenterServerAddress.KEY_HEADER_STRING,CenterServerAddress.KEY_SECUREUTIL_MD5)
                .body(JSON.toJSONString(replaceRfidVo))
                .timeout(50000)//超时，毫秒
                .execute().body();

        return getResultMessage(result);
    }

    public Object deleteRFID(DeleteRFIDVo deleteRFIDVo){
        AgedmallGoodsEquExample goodsEquExample=new AgedmallGoodsEquExample();
        for (Long id:deleteRFIDVo.getIdList()) {
            //解除当前rfid绑定
            agedmallLgRfidStateService.addLgRfidGoods(0L,id.toString(),1);
        }
        goodsEquExample.or().andEquIdIn(deleteRFIDVo.getIdList()).andDeletedEqualTo((byte)0).andTypeEqualTo(0);
        List<AgedmallGoodsEqu> goodsEqus = agedmallGoodsEquMapper.selectByExample(goodsEquExample);
        agedmallGoodsEquMapper.logicalDeleteByExample(goodsEquExample);
        //修改agedmall_lg_rfid_goods_cur_stat状态

        String result=HttpRequest.post(serverAddress.getCenter()+"admin_api/deleteRFID")
                //头信息，多个头信息多次调用此方法即可
                .header(CenterServerAddress.KEY_HEADER_STRING,CenterServerAddress.KEY_SECUREUTIL_MD5)
                .body(JSON.toJSONString(deleteRFIDVo.getIdList()))
                .timeout(50000)//超时，毫秒
                .execute().body();
        for (int i = 0; i < goodsEqus.size(); i++) {
            AgedmallGoodsEqu goodsEqu=goodsEqus.get(i);
            inventoryUtil.remove(goodsEqu.getStoreId().intValue(),0,1,goodsEqu.getGoodsId().intValue()
                    ,goodsEqu.getGoodsProductId().intValue());
        }
        return getResultMessage(result);
    }

    public Object deleteControl(DeleteControlVo deleteControlVo){
        AgedmallGoodsEquExample goodsEquExample=new AgedmallGoodsEquExample();
        goodsEquExample.or().andEquIdIn(deleteControlVo.getIdList()).andDeletedEqualTo((byte)0).andTypeEqualTo(1);
        agedmallGoodsEquMapper.logicalDeleteByExample(goodsEquExample);
        String result=HttpRequest.post(serverAddress.getCenter()+"admin_api/deleteControl")
                //头信息，多个头信息多次调用此方法即可
                .header(CenterServerAddress.KEY_HEADER_STRING,CenterServerAddress.KEY_SECUREUTIL_MD5)
                .body(JSON.toJSONString(deleteControlVo.getIdList()))
                .timeout(50000)//超时，毫秒
                .execute().body();
        return getResultMessage(result);
    }



    public Object addRFIDCommodity(AddRFIDCommodityVo addRFIDCommodityVo){
        List<AgedmallRfidGoodsProduct> rfidGoodsProducts=new ArrayList<>();
        List<String> rfids= Arrays.asList(addRFIDCommodityVo.getRfidCode().split(","));
        AgedmallRfidGoodsProductExample goodsProductExample=new AgedmallRfidGoodsProductExample();
        goodsProductExample.or().andRfidIn(rfids).andDeletedEqualTo(false);
        List<AgedmallRfidGoodsProduct> list = agedmallRfidGoodsProductMapper.selectByExample(goodsProductExample);
        if(list!=null&&!list.isEmpty()){
            return ResponseUtil.fail(AdminErrorCode.RFID_IS_EXIST.getCode(),AdminErrorCode.RFID_IS_EXIST.getMessage());
        }
        long count = list.stream().distinct().count();
        boolean isRepeat = count < list.size();
        if(isRepeat){
            return ResponseUtil.fail(AdminErrorCode.RFID_CANT_REPEAT.getCode(),
                    AdminErrorCode.RFID_CANT_REPEAT.getMessage());
        }
        List<LitemallGoodsProductSpecification> skuFormatDtos=new ArrayList<>();
        for (int i = 0; i < addRFIDCommodityVo.getSkuInfo().size(); i++) {
            SkuInfoVo skuInfoVo = addRFIDCommodityVo.getSkuInfo().get(i);
            LitemallGoodsProductSpecification formatDto=new LitemallGoodsProductSpecification();
            formatDto.setId(skuInfoVo.getSpecId());
            formatDto.setName(skuInfoVo.getSpecName());
            formatDto.setInfoId(skuInfoVo.getSkuId());
            formatDto.setInfoName(skuInfoVo.getSkuName());
            skuFormatDtos.add(formatDto);
        }
        LitemallGoodsProduct  GoodsProduct=new LitemallGoodsProduct();
        if(!addRFIDCommodityVo.getSkuInfo().isEmpty()){
            //判断是否存在
            LitemallGoodsProductExample goodsIdProductExample=new LitemallGoodsProductExample();
            goodsIdProductExample.or().andGoodsIdEqualTo(addRFIDCommodityVo.getCommodityId());
            List<LitemallGoodsProduct> litemallGoodsProduct = litemallGoodsProductMapper.selectByExample(goodsIdProductExample);
            for (int i = 0; i < litemallGoodsProduct.size(); i++) {
                boolean isTrue=true;
                List<LitemallGoodsProductSpecification> productSpecifications=
                        JSONArray.parseArray(litemallGoodsProduct.get(i).getSpecifications(),LitemallGoodsProductSpecification.class);
                for (int k = 0; k < skuFormatDtos.size(); k++) {
                    if(!productSpecifications.contains(skuFormatDtos.get(k))){
                        isTrue=false;
                        break;
                    }
                }
                if(isTrue){
                    GoodsProduct=litemallGoodsProduct.get(i);
                }
            }
            if(ObjectUtil.isNull(GoodsProduct)||GoodsProduct.getId()==null){
                return ResponseUtil.fail(AdminErrorCode.GOODS_PRODUCT_NOT_EXIST.getCode()
                        ,AdminErrorCode.GOODS_PRODUCT_NOT_EXIST.getMessage());
            }
        }
        AgedmallStore store=agedmallStoreMapper.selectByPrimaryKey(addRFIDCommodityVo.getStoreId());
        for (int i = 0; i < rfids.size(); i++) {
            String rfid=rfids.get(i);
            AgedmallRfidGoodsProduct agedmallRfidGoodsProduct=new AgedmallRfidGoodsProduct();
            agedmallRfidGoodsProduct.setRfid(rfid);
            agedmallRfidGoodsProduct.setStoreId(addRFIDCommodityVo.getStoreId());
            agedmallRfidGoodsProduct.setBusiId(store.getBusiId());
            agedmallRfidGoodsProduct.setGoodsId(addRFIDCommodityVo.getCommodityId().longValue());
            agedmallRfidGoodsProduct.setSkuName(addRFIDCommodityVo.getSkuName());
            agedmallRfidGoodsProduct.setGoodsProductId(ObjectUtil.isNull(GoodsProduct)||GoodsProduct.getId()==null?null:GoodsProduct.getId().longValue());
            agedmallRfidGoodsProduct.setStat(0);
            agedmallRfidGoodsProduct.setOnlineTime(LocalDateTime.now());
            agedmallRfidGoodsProduct.setAddTime(LocalDateTime.now());
            rfidGoodsProducts.add(agedmallRfidGoodsProduct);
            //新增agedmall_lg_rfid_goods_cur_stat
            agedmallLgRfidStateService.addLgRfidGoods(addRFIDCommodityVo.getStoreId(),rfid,2);
        }
        agedmallLocksMapper.insertRfidGoodsProductList(rfidGoodsProducts);

        inventoryUtil.refresh(addRFIDCommodityVo.getStoreId().intValue(),0);
        return ResponseUtil.ok();
    }


    public Integer rfidGoodsStatChange(String state){
        if(StrUtil.isBlank(state)){
            return null;
        }
        switch (state){
            case "low":
                return 1;
            case "using":
                return 3;
            case "defending":
                return 2;
            case "sold":
                return 4;
            case "up":
            default:
                return 0;
        }
    }


    public Integer rfidListDbToWeb(Integer state){
        switch (state){
            case 0:
                return 1;
            case 1:
            case 5:
            case 6:
                return 3;
            case 2:
                return 2;
            case 3:
                return 4;
            case 4:
                return 4;
            default:
                return 0;
        }
    }

    public Object rfidCommodityList(RFIDCommodityListVo rfidCommodityListVo){
        if(ObjectUtil.isNull(rfidCommodityListVo.getAddress())){
            rfidCommodityListVo.setAddress(new ChainAddress());
        }
        SelectRfidGoodsDto selectRfidGoodsDto=new SelectRfidGoodsDto();
        selectRfidGoodsDto.setRfid(rfidCommodityListVo.getRfidCode());
        selectRfidGoodsDto.setProvinceId(rfidCommodityListVo.getAddress().getProvince());
        selectRfidGoodsDto.setCityId(rfidCommodityListVo.getAddress().getCity());
        selectRfidGoodsDto.setAreaId(rfidCommodityListVo.getAddress().getArea());
        selectRfidGoodsDto.setStat(rfidCommodityListVo.getState()!=null?Integer.parseInt(rfidCommodityListVo.getState()):null);
        selectRfidGoodsDto.setStoreId(rfidCommodityListVo.getStoreId());
        selectRfidGoodsDto.setGoodsId(rfidCommodityListVo.getCommdityId());
        selectRfidGoodsDto.setShopId(rfidCommodityListVo.getShopId());
        selectRfidGoodsDto.setMedicalId(rfidCommodityListVo.getMedicalId());
        Page page= PageHelper.startPage(rfidCommodityListVo.getPage(),rfidCommodityListVo.getLimit());
        List<RfidGoodsListBack> rfidGoodsListBacks = agedmallLocksMapper.selectRfidGoodsList(selectRfidGoodsDto);
        for (int i = 0; i < rfidGoodsListBacks.size(); i++) {
            rfidGoodsListBacks.get(i).setState(rfidListDbToWeb(rfidGoodsListBacks.get(i).getState()));
        }
        Map<String,Object> map=new HashMap<>();
        map.put("total",page.getTotal());
        map.put("data",rfidGoodsListBacks);
        return ResponseUtil.ok(map);
    }

    public Object changeRFIDCommodityRFID(ChangeRFIDCommodityRFIDVo changeRFIDCommodityRFIDVo){

        AgedmallRfidGoodsProductExample newGoodsProduct=new AgedmallRfidGoodsProductExample();
        newGoodsProduct.or().andRfidEqualTo(changeRFIDCommodityRFIDVo.getNewRfidCode());
        AgedmallRfidGoodsProduct  isExistNewRfid= agedmallRfidGoodsProductMapper.selectOneByExample(newGoodsProduct);
        if(ObjectUtil.isNotNull(isExistNewRfid)){
            return ResponseUtil.fail(AdminErrorCode.RFID_IS_EXIST.getCode(),
                    AdminErrorCode.RFID_IS_EXIST.getMessage());
        }
        AgedmallRfidGoodsProductExample goodsProductExample=new AgedmallRfidGoodsProductExample();
        goodsProductExample.or().andRfidEqualTo(changeRFIDCommodityRFIDVo.getOldRfidCode());
        AgedmallRfidGoodsProduct agedmallRfidGoodsProduct = agedmallRfidGoodsProductMapper.selectOneByExample(goodsProductExample);
        if(ObjectUtil.isNull(agedmallRfidGoodsProduct)||agedmallRfidGoodsProduct.getId()==null){
            return ResponseUtil.fail(AdminErrorCode.OLD_RFID_NOT_EXIST.getCode(),
                    AdminErrorCode.OLD_RFID_NOT_EXIST.getMessage());
        }
        agedmallRfidGoodsProduct.setRfid(changeRFIDCommodityRFIDVo.getNewRfidCode());
        agedmallRfidGoodsProductMapper.updateByPrimaryKeySelective(agedmallRfidGoodsProduct);
        return ResponseUtil.ok();
    }


    public Object changeRFIDCommodityState(ChangeRFIDCommodityStateVo  changeRFIDCommodityStateVo){
        List<AgedmallRfidGoodsProduct> list=new ArrayList<>();
        for (String rfid:changeRFIDCommodityStateVo.getIdList()) {
            //修改agedmall_lg_rfid_goods_cur_stat状态
            agedmallLgRfidStateService.changeLgRfidState(rfid,exChangeRfidToLgRfidState(changeRFIDCommodityStateVo.getType()),2);
        }
            AgedmallRfidGoodsProductExample rfidGoodsProductExample=new AgedmallRfidGoodsProductExample();
            rfidGoodsProductExample.or().andRfidIn(changeRFIDCommodityStateVo.getIdList());
        List<AgedmallRfidGoodsProduct> rfidGoodsProducts = agedmallRfidGoodsProductMapper.selectByExample(rfidGoodsProductExample);
        for (int i = 0; i < rfidGoodsProducts.size(); i++) {
            AgedmallRfidGoodsProduct rfidGoodsProduct=rfidGoodsProducts.get(i);
            if(changeRFIDCommodityStateVo.getType()!=2){
                rfidGoodsProduct.setMaintainReason("");
            }
            if(changeRFIDCommodityStateVo.getType()==0){
                rfidGoodsProduct.setOnlineTime(LocalDateTime.now());
            }
            else if(changeRFIDCommodityStateVo.getType()==2){
                rfidGoodsProduct.setMaintainReason(changeRFIDCommodityStateVo.getReason());
            }
            rfidGoodsProduct.setStat(changeRFIDCommodityStateVo.getType());
            list.add(rfidGoodsProduct);
        }
        if(!list.isEmpty()){
        agedmallLocksMapper.updateRfidGoodsBatch(list);
        }
        for (int i = 0; i < rfidGoodsProducts.size(); i++) {
            AgedmallRfidGoodsProduct inventoryGoods=rfidGoodsProducts.get(i);
            if(changeRFIDCommodityStateVo.getType()==0){
                inventoryUtil.add(inventoryGoods.getStoreId().intValue(),0,2,inventoryGoods.getGoodsId().intValue()
                        ,inventoryGoods.getGoodsProductId().intValue());
            }
            else{
                inventoryUtil.remove(inventoryGoods.getStoreId().intValue(),0,2,inventoryGoods.getGoodsId().intValue()
                        ,inventoryGoods.getGoodsProductId().intValue());
            }
        }
        return ResponseUtil.ok();
    }


    public Object deleteRFIDCommodity( DeleteRfidCommodityVo deleteRfidCommodityVo){
        AgedmallRfidGoodsProductExample goodsProductExample=new AgedmallRfidGoodsProductExample();
        goodsProductExample.or().andRfidIn(deleteRfidCommodityVo.getIdList()).andDeletedEqualTo(false);
        List<AgedmallRfidGoodsProduct> list = agedmallRfidGoodsProductMapper.selectByExample(goodsProductExample);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setDeleted(true);
            agedmallRfidGoodsProductMapper.updateByPrimaryKeySelective(list.get(i));
        }
        if(list.isEmpty()){
            return ResponseUtil.fail(AdminErrorCode.RFID_NOT_EXIST.getCode(),AdminErrorCode.RFID_NOT_EXIST.getMessage());
        }
        for (int i = 0; i < list.size(); i++) {
            AgedmallRfidGoodsProduct inventoryGoods=list.get(i);
            //修改agedmall_lg_rfid_goods_cur_stat
            agedmallLgRfidStateService.addLgRfidGoods(0L,inventoryGoods.getRfid(),2);
            inventoryUtil.remove(inventoryGoods.getStoreId().intValue(),0,2,inventoryGoods.getGoodsId().intValue()
                    ,inventoryGoods.getGoodsProductId().intValue());
        }
        return ResponseUtil.ok();
    }


    public Object addMedicalLock(AddMedicalLockVo addMedicalLockVo){
        List<AgedmallCabinet> agedmallCabinets=new ArrayList<>();
        List<String> numbers=Arrays.asList(addMedicalLockVo.getMedicalCode().split(","));
        if(numbers.size()>1){
            return ResponseUtil.fail(AdminErrorCode.STORE_ONLY_ONE_MEDICAL_LOCKS.getCode(),
                    AdminErrorCode.STORE_ONLY_ONE_MEDICAL_LOCKS.getMessage());
        }
        AgedmallCabinetExample cabinetExample=new AgedmallCabinetExample();
        cabinetExample.or().andRfidIn(numbers).andStoreIdEqualTo(addMedicalLockVo.getStoreId().longValue()).andDeletedEqualTo((byte)0);
        List<AgedmallCabinet> isExistCabinetList = agedmallCabinetMapper.selectByExample(cabinetExample);
        if(!isExistCabinetList.isEmpty()){
            return ResponseUtil.fail(AdminErrorCode.MEDICAL_LOCKS_IS_EXIST.getCode(),AdminErrorCode.MEDICAL_LOCKS_IS_EXIST.getMessage());
        }
        long count = numbers.stream().distinct().count();
        boolean isRepeat = count < numbers.size();
        if(isRepeat){
            return ResponseUtil.fail(AdminErrorCode.RFID_CANT_REPEAT.getCode(),
                    AdminErrorCode.RFID_CANT_REPEAT.getMessage());
        }
        for (int i = 0; i < numbers.size(); i++) {
            AgedmallCabinet agedmallCabinet=new AgedmallCabinet();
            agedmallCabinet.setAddTime(LocalDateTime.now());
            agedmallCabinet.setOnlineTime(LocalDateTime.now());
            agedmallCabinet.setStoreId(addMedicalLockVo.getStoreId().longValue());
            agedmallCabinet.setPosition(addMedicalLockVo.getPosition());
            AgedmallStorePositionExample storePositionExample=new AgedmallStorePositionExample();
            storePositionExample.or().andPositionEqualTo(addMedicalLockVo.getPosition()).andStoreIdEqualTo(addMedicalLockVo.getStoreId());
            AgedmallStorePosition storePosition=agedmallStorePositionMapper.selectOneByExample(storePositionExample);
            if(storePosition==null){
                AgedmallStorePosition agedmallStorePosition=new AgedmallStorePosition();
                agedmallStorePosition.setStoreId(addMedicalLockVo.getStoreId());
                agedmallStorePosition.setPosition(addMedicalLockVo.getPosition());
                agedmallStorePosition.setAddTime(LocalDateTime.now());
                agedmallStorePositionMapper.insertSelective(agedmallStorePosition);
            }
            agedmallCabinet.setHardwareVersionId(addMedicalLockVo.getHversion());
            agedmallCabinet.setSoftwareVersionId(addMedicalLockVo.getDversion());
            agedmallCabinet.setRfid(numbers.get(i));
            agedmallCabinet.setStat(0);
            agedmallCabinets.add(agedmallCabinet);
        }
        agedmallLocksMapper.insertCabinetBatch(agedmallCabinets);
        return ResponseUtil.ok();
    }

    public Object medicalLockList(MedicalLockListVo medicalLockListVo){
        if(ObjectUtil.isNull(medicalLockListVo.getAddress())){
            medicalLockListVo.setAddress(new ChainAddress());
        }
        SelectCabinetDto selectRfidGoodsDto=new SelectCabinetDto();
        selectRfidGoodsDto.setRfid(medicalLockListVo.getMedicalCode());
        selectRfidGoodsDto.setProvinceId(medicalLockListVo.getAddress().getProvince());
        selectRfidGoodsDto.setCityId(medicalLockListVo.getAddress().getCity());
        selectRfidGoodsDto.setAreaId(medicalLockListVo.getAddress().getArea());
        selectRfidGoodsDto.setStat(medicalLockListVo.getState());
        selectRfidGoodsDto.setStoreId(medicalLockListVo.getStoreId());
        selectRfidGoodsDto.setShopId(medicalLockListVo.getShopId());
        Page page= PageHelper.startPage(medicalLockListVo.getPage(),medicalLockListVo.getLimit());
        List<CabinetListBackDto> cabinetListBackDtos = agedmallLocksMapper.selectCabinetList(selectRfidGoodsDto);
        Map<String,Object> map=new HashMap<>();
        map.put("total",page.getTotal());
        map.put("data",cabinetListBackDtos);
        return ResponseUtil.ok(map);
    }


    public Object changeMedicalLockState(ChangeMedicalLockStateVo changeMedicalLockStateVo){
        List<AgedmallCabinet> list=new ArrayList<>();
        AgedmallCabinetExample agedmallCabinetExample=new AgedmallCabinetExample();
        agedmallCabinetExample.or().andIdIn(changeMedicalLockStateVo.getIdList());
        List<AgedmallCabinet> agedmallCabinets = agedmallCabinetMapper.selectByExample(agedmallCabinetExample);
        for (int i = 0; i < agedmallCabinets.size(); i++) {
            AgedmallCabinet cabinet=agedmallCabinets.get(i);
            if(changeMedicalLockStateVo.getType()!=2){
                cabinet.setMaintainReason("");
            }
            if(changeMedicalLockStateVo.getType()==0){
                cabinet.setOnlineTime(LocalDateTime.now());
            }
            else if(changeMedicalLockStateVo.getType()==2){
                cabinet.setMaintainReason(changeMedicalLockStateVo.getReason());
            }
            cabinet.setStat(changeMedicalLockStateVo.getType());
            list.add(cabinet);
        }
        if(!list.isEmpty()){
        agedmallLocksMapper.updateCabinetBatch(list);
        }
        else{
            return ResponseUtil.fail(AdminErrorCode.MEDICAL_LOCKS_NOT_EXIST.getCode()
                    ,AdminErrorCode.MEDICAL_LOCKS_NOT_EXIST.getMessage());
        }
        return ResponseUtil.ok();
    }

    public Object deleteMedicalLock(DeleteMedicalLockVo deleteMedicalLockVo){
        AgedmallCabinetExample cabinetExample=new AgedmallCabinetExample();
        cabinetExample.or().andIdIn(deleteMedicalLockVo.getIdList()).andDeletedEqualTo((byte)0);
        List<AgedmallCabinet> list = agedmallCabinetMapper.selectByExample(cabinetExample);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setDeleted((byte)1);
        }
        if(list.isEmpty()){
            return ResponseUtil.fail(AdminErrorCode.MEDICAL_LOCKS_NOT_EXIST.getCode()
                    ,AdminErrorCode.MEDICAL_LOCKS_NOT_EXIST.getMessage());
        }
        agedmallLocksMapper.updateCabinetBatch(list);
        return ResponseUtil.ok();
    }

    //0-硬件 1-软件
    public Object getMedicalVersionList(Integer versionType){
        AgedmallVersionExample agedmallVersionExample=new AgedmallVersionExample();
        agedmallVersionExample.or().andTypeEqualTo(1).andVersionTypeEqualTo(versionType);
        List<AgedmallVersion> agedmallVersions = agedmallVersionMapper.selectByExample(agedmallVersionExample);
        return ResponseUtil.ok(agedmallVersions);
    }

    public Object addBargun(AddBargunVo addBargunVo){
        List<AgedmallScanGuns> agedmallScanGuns=new ArrayList<>();
        List<String> numbers=Arrays.asList(addBargunVo.getBargunCode().split(","));
        AgedmallScanGunsExample scanGunsExample=new AgedmallScanGunsExample();
        scanGunsExample.or().andRfidIn(numbers).andDeletedEqualTo((byte)0);
        List<AgedmallScanGuns> isExistScanGuns = agedmallScanGunsMapper.selectByExample(scanGunsExample);
        if(!isExistScanGuns.isEmpty()){
            return ResponseUtil.fail(AdminErrorCode.SCAN_GUNS_IS_BINDING.getCode(),AdminErrorCode.SCAN_GUNS_IS_BINDING.getMessage());
        }
        long count = numbers.stream().distinct().count();
        boolean isRepeat = count < numbers.size();
        if(isRepeat){
            return ResponseUtil.fail(AdminErrorCode.RFID_CANT_REPEAT.getCode(), AdminErrorCode.RFID_CANT_REPEAT.getMessage());
        }
        for (int i = 0; i < numbers.size(); i++) {
            AgedmallScanGuns scanGun=new AgedmallScanGuns();
            scanGun.setAddTime(LocalDateTime.now());
            scanGun.setStoreId(addBargunVo.getStoreId().longValue());
            scanGun.setPosition(addBargunVo.getPosition());
            AgedmallStorePositionExample storePositionExample=new AgedmallStorePositionExample();
            storePositionExample.or().andPositionEqualTo(addBargunVo.getPosition()).andStoreIdEqualTo(addBargunVo.getStoreId());
            AgedmallStorePosition storePosition=agedmallStorePositionMapper.selectOneByExample(storePositionExample);
            if(storePosition==null){
                AgedmallStorePosition agedmallStorePosition=new AgedmallStorePosition();
                agedmallStorePosition.setStoreId(addBargunVo.getStoreId());
                agedmallStorePosition.setPosition(addBargunVo.getPosition());
                agedmallStorePosition.setAddTime(LocalDateTime.now());
                agedmallStorePositionMapper.insertSelective(agedmallStorePosition);
            }
            scanGun.setRfid(numbers.get(i));
            scanGun.setStat(0);
            scanGun.setOnlineTime(LocalDateTime.now());
            agedmallScanGuns.add(scanGun);
        }
        agedmallLocksMapper.insertScanGunsBatch(agedmallScanGuns);
        return ResponseUtil.ok();
    }



    public Object medicalLockDetail(MedicalLockDetailVo medicalLockDetailVo){
        SelectCabinetGoodsVo selectCabinetGoodsVo=new SelectCabinetGoodsVo();
        selectCabinetGoodsVo.setId(medicalLockDetailVo.getMedicalLockId());
        selectCabinetGoodsVo.setRfid(medicalLockDetailVo.getRfidCode());
        selectCabinetGoodsVo.setGoodsId(medicalLockDetailVo.getCommdityId());
        selectCabinetGoodsVo.setStat(rfidGoodsStatChange(medicalLockDetailVo.getState()));
        Page page= PageHelper.startPage(medicalLockDetailVo.getPage(),medicalLockDetailVo.getLimit());
        List<CabinetGoodsDetailListDto> cabinetGoodsDetailListDtos = agedmallLocksMapper.selectRfidGoodsListByCabinetId(selectCabinetGoodsVo);
        Map<String,Object> map=new HashMap<>();
        map.put("total",page.getTotal());
        map.put("data",cabinetGoodsDetailListDtos);
        return ResponseUtil.ok(map);
    }

    public Object bargunList(BargunListVo bargunListVo){
        if(ObjectUtil.isNull(bargunListVo.getAddress())){
            bargunListVo.setAddress(new ChainAddress());
        }
        SelectScanGunsList selectCabinetGoodsVo=new SelectScanGunsList();
        selectCabinetGoodsVo.setProvinceId(bargunListVo.getAddress().getProvince());
        selectCabinetGoodsVo.setCityId(bargunListVo.getAddress().getCity());
        selectCabinetGoodsVo.setAreaId(bargunListVo.getAddress().getArea());
        selectCabinetGoodsVo.setStat(bargunListVo.getState());
        selectCabinetGoodsVo.setRfid(bargunListVo.getBargunCode());
        selectCabinetGoodsVo.setStoreId(bargunListVo.getStoreId());
        selectCabinetGoodsVo.setShopId(bargunListVo.getShopId());
        Page page= PageHelper.startPage(bargunListVo.getPage(),bargunListVo.getLimit());
        List<ScanGunsListDto> scanGunsListDtos = agedmallLocksMapper.selectScanGunsList(selectCabinetGoodsVo);
        Map<String,Object> map=new HashMap<>();
        map.put("total",page.getTotal());
        map.put("data",scanGunsListDtos);
        return ResponseUtil.ok(map);
    }

    public Object changeBargunState(ChangeBargunStateVo changeBargunStateVo){
        List<AgedmallScanGuns> list=new ArrayList<>();
        AgedmallScanGunsExample agedmallScanGuns=new AgedmallScanGunsExample();
        agedmallScanGuns.or().andRfidIn(changeBargunStateVo.getIdList()).andDeletedEqualTo((byte)0);
        List<AgedmallScanGuns> scanGunsList = agedmallScanGunsMapper.selectByExample(agedmallScanGuns);
        for (int i = 0; i < scanGunsList.size(); i++) {
            AgedmallScanGuns scanGuns=scanGunsList.get(i);
            if(changeBargunStateVo.getType()!=2){
                scanGuns.setMaintainReason("");
            }
            if(changeBargunStateVo.getType()==0){
                scanGuns.setOnlineTime(LocalDateTime.now());
            }
            else if(changeBargunStateVo.getType()==2){
                scanGuns.setMaintainReason(changeBargunStateVo.getReason());
            }
            scanGuns.setStat(changeBargunStateVo.getType());
            list.add(scanGuns);
        }
        if(!scanGunsList.isEmpty()){
            agedmallLocksMapper.updateScanGunsBatch(list);
        }
        else{
            return ResponseUtil.fail(AdminErrorCode.SCAN_GUNS_NOT_EXIST.getCode(), AdminErrorCode.SCAN_GUNS_NOT_EXIST.getMessage());
        }
        return ResponseUtil.ok();
    }

    public Object deleteBargun(DeleteBargunVo deleteBargunVo){
        AgedmallScanGunsExample scanGunsExample=new AgedmallScanGunsExample();
        scanGunsExample.or().andRfidIn(deleteBargunVo.getIdList()).andDeletedEqualTo((byte)0);
        List<AgedmallScanGuns> list = agedmallScanGunsMapper.selectByExample(scanGunsExample);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setDeleted((byte)1);
        }
        if(list.isEmpty()){
            return ResponseUtil.fail(AdminErrorCode.SCAN_GUNS_NOT_EXIST.getCode(), AdminErrorCode.SCAN_GUNS_NOT_EXIST.getMessage());
        }
        agedmallLocksMapper.updateScanGunsBatch(list);
        return ResponseUtil.ok();
    }


    public Object lockGoods(LockGoodsVo lockGoodsVo){
        if(lockGoodsVo.getNumber()==null){
            lockGoodsVo.setNumber(1);
        }
        AgedmallGoodsEquExample goodsEquExample=new AgedmallGoodsEquExample();
        goodsEquExample.or().andDeletedEqualTo((byte)0).andStoreIdEqualTo(lockGoodsVo.getStoreId()).
                andGoodsIdEqualTo(lockGoodsVo.getGoodsId().longValue()).andTypeEqualTo(0).andStateEqualTo(0);
        List<AgedmallGoodsEqu> goodsEquList = agedmallGoodsEquMapper.selectByExample(goodsEquExample);
        if(ObjectUtil.isNull(goodsEquList)){
            return ResponseUtil.fail(AdminErrorCode.INVENTORY_IS_NOT_EXIST.getCode(), AdminErrorCode.INVENTORY_IS_NOT_EXIST.getMessage());
        }
        if(goodsEquList.size()<lockGoodsVo.getNumber()){
            return ResponseUtil.fail(AdminErrorCode.INVENTORY_IS_NOT_FULL.getCode(), AdminErrorCode.INVENTORY_IS_NOT_FULL.getMessage());
        }
        List<Long> backEquIds=new ArrayList<>();
        for (int i = 0; i < lockGoodsVo.getNumber(); i++) {
            AgedmallGoodsEqu goodsEqu=goodsEquList.get(i);
            //锁定库存
            inventoryUtil.remove(lockGoodsVo.getStoreId().intValue(),0,1,lockGoodsVo.getGoodsId(),lockGoodsVo.getSpecId());
            //锁定商品
            goodsEqu.setState(4);
            agedmallGoodsEquMapper.updateByPrimaryKey(goodsEqu);
            backEquIds.add(goodsEqu.getEquId());
            //修改agedmall_lg_rfid_goods_cur_stat状态
            agedmallLgRfidStateService.changeLgRfidState(goodsEqu.getEquId().toString(),1,1);
        }

        return ResponseUtil.ok(backEquIds);
    }


    public Object unlockGoods(UnLockGoodsVo unLockGoodsVo){
        AgedmallGoodsEquExample goodsEquExample=new AgedmallGoodsEquExample();
        goodsEquExample.or().andDeletedEqualTo((byte)0).andEquIdIn(unLockGoodsVo.getRfid()).andTypeEqualTo(0);
        List<AgedmallGoodsEqu> goodsEquList = agedmallGoodsEquMapper.selectByExample(goodsEquExample);
        for (AgedmallGoodsEqu goodsEqu:goodsEquList) {
            goodsEqu.setState(0);
            //库存增加
            inventoryUtil.add(goodsEqu.getStoreId().intValue(),0,1,goodsEqu.getGoodsId().intValue()
                    ,goodsEqu.getGoodsProductId().intValue());
            //解锁商品
            agedmallGoodsEquMapper.updateByPrimaryKey(goodsEqu);
            //修改agedmall_lg_rfid_goods_cur_stat状态
            agedmallLgRfidStateService.changeLgRfidState(goodsEqu.getEquId().toString(),0,1);
        }
        return ResponseUtil.ok();
    }


    public Object lockCabinetGoods(LockGoodsVo lockGoodsVo){
        if(lockGoodsVo.getNumber()==null){
            lockGoodsVo.setNumber(1);
        }
        AgedmallCabinetExample cabinetExample=new AgedmallCabinetExample();
        cabinetExample.or().andDeletedEqualTo((byte)0).andStoreIdEqualTo(lockGoodsVo.getStoreId());
        AgedmallCabinet cabinet = agedmallCabinetMapper.selectOneByExample(cabinetExample);
        if(ObjectUtil.isNull(cabinet)){
            return ResponseUtil.fail(AdminErrorCode.MEDICAL_LOCKS_NOT_EXIST.getCode(), AdminErrorCode.MEDICAL_LOCKS_NOT_EXIST.getMessage());
        }
        List<CanUseCabinetGoodsDto> canUseCabinetGoodsDtos = agedmallLocksMapper.selectCanUseCabinetGoodsId(cabinet.getId()
                , lockGoodsVo.getStoreId(), lockGoodsVo.getGoodsId().longValue(),lockGoodsVo.getSpecId().longValue());
        if(ObjectUtil.isNull(canUseCabinetGoodsDtos)){
            return ResponseUtil.fail(AdminErrorCode.INVENTORY_IS_NOT_EXIST.getCode(), AdminErrorCode.INVENTORY_IS_NOT_EXIST.getMessage());
        }
        if(canUseCabinetGoodsDtos.size()<lockGoodsVo.getNumber()){
            return ResponseUtil.fail(AdminErrorCode.INVENTORY_IS_NOT_FULL.getCode(), AdminErrorCode.INVENTORY_IS_NOT_FULL.getMessage());
        }
        List<String> rfidList=new ArrayList<>();
        for (int i = 0; i < lockGoodsVo.getNumber(); i++) {
            CanUseCabinetGoodsDto canUseCabinetGoodsDto=canUseCabinetGoodsDtos.get(i);
            //锁定库存
            inventoryUtil.remove(lockGoodsVo.getStoreId().intValue(),0,2,lockGoodsVo.getGoodsId(),lockGoodsVo.getSpecId());
            //锁定商品
            AgedmallCabinetGoodsExample goodsExample=new AgedmallCabinetGoodsExample();
            goodsExample.or().andIdEqualTo(canUseCabinetGoodsDto.getCanbinetGoodsId());
            AgedmallCabinetGoods cabinetGoods=new AgedmallCabinetGoods();
            cabinetGoods.setStat(3);
            agedmallCabinetGoodsMapper.updateByExample(cabinetGoods,goodsExample);

            AgedmallRfidGoodsProductExample goodsProductExample=new AgedmallRfidGoodsProductExample();
            goodsProductExample.or().andIdEqualTo(canUseCabinetGoodsDto.getRfidGoodsProductId());
            AgedmallRfidGoodsProduct goodsProduct=new AgedmallRfidGoodsProduct();
            goodsProduct.setStat(5);
            agedmallRfidGoodsProductMapper.updateByExample(goodsProduct,goodsProductExample);
            rfidList.add(canUseCabinetGoodsDto.getRfid());
            //修改agedmall_lg_rfid_goods_cur_stat状态
            agedmallLgRfidStateService.changeLgRfidState(canUseCabinetGoodsDto.getRfid(),1,2);
        }


        return ResponseUtil.ok(rfidList);
    }


    public Object unlockCabinetGoods(UnLockCabinetGoodsVo unLockGoodsVo){
        AgedmallRfidGoodsProductExample goodsProductExample=new AgedmallRfidGoodsProductExample();
        goodsProductExample.or().andRfidIn(unLockGoodsVo.getRfid()).andDeletedEqualTo(false);
        List<AgedmallRfidGoodsProduct> agedmallRfidGoodsProductList = agedmallRfidGoodsProductMapper.selectByExample(goodsProductExample);

        for (AgedmallRfidGoodsProduct agedmallRfidGoodsProduct:agedmallRfidGoodsProductList) {
            //库存增加
            inventoryUtil.add(agedmallRfidGoodsProduct.getStoreId().intValue(),0,2,agedmallRfidGoodsProduct.getGoodsId().intValue()
                    ,agedmallRfidGoodsProduct.getGoodsProductId().intValue());
            //解锁商品
            AgedmallCabinetGoodsExample goodsExample=new AgedmallCabinetGoodsExample();
            goodsExample.or().andDeletedEqualTo(false).andGoodsRfidIdEqualTo(agedmallRfidGoodsProduct.getId());
            AgedmallCabinetGoods cabinetGoods=new AgedmallCabinetGoods();
            cabinetGoods.setStat(0);
            agedmallCabinetGoodsMapper.updateByExample(cabinetGoods,goodsExample);
            agedmallRfidGoodsProduct.setStat(0);
            agedmallRfidGoodsProductMapper.updateByExample(agedmallRfidGoodsProduct,goodsProductExample);
            //修改agedmall_lg_rfid_goods_cur_stat状态
            agedmallLgRfidStateService.changeLgRfidState(agedmallRfidGoodsProduct.getRfid(),0,2);
        }

        return ResponseUtil.ok();
    }


}
